﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Draco.OWF.Acticity.General;
using Draco.OWF.Studio.Utility;
using Draco.OWF.Transition;

namespace Draco.OWF.Studio.Designer
{
    /// <summary>
    /// Interaction logic for DesignerCanvas.xaml
    /// </summary>
    public partial class DesignerVisualPanel : UserControl, IProcessDefinitionEditor
    {
        public IDesignerEnv DesignerEnv{get;private set;}
        private IProcess m_ProcessDefinition;
        public DesignerVisualPanel()
        {
            this.DesignerEnv = new DesignerEnv();

            InitializeComponent();
            this.CoordinateCanvas.InitCoordinate(DesignerEnv,this);
            ElementMoveWrapper wrapper = new ElementMoveWrapper(this.MainCanvas,this);
            wrapper.ElementMoving += new ElementMoveEventHandler(wrapper_ElementMove);
        }

        #region Canvas operation
        /// <summary>
        /// Move canvas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void wrapper_ElementMove(object sender, ElementMoveEventArgs e)
        {
            double x = Canvas.GetLeft(this.CoordinateCanvas)+e.DeltaX;
            double y = Canvas.GetTop(this.CoordinateCanvas)+e.DeltaY;
            Canvas.SetLeft(this.CoordinateCanvas, x);
            Canvas.SetTop(this.CoordinateCanvas, y);
            Point coordinate2Ruler = new Point(-x + this.Scroller.HorizontalOffset, -y + this.Scroller.VerticalOffset);
            this.HorizontalRuler.Origin = coordinate2Ruler;
            this.VerticalRuler.Origin = coordinate2Ruler;
        }
        /// <summary>
        /// scroll canvas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Scroller_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            double x = Canvas.GetLeft(this.CoordinateCanvas);
            double y = Canvas.GetTop(this.CoordinateCanvas);
            Point coordinate2Ruler = new Point(-x + e.HorizontalOffset, -y + e.VerticalOffset);
            this.HorizontalRuler.Origin = coordinate2Ruler;
            this.VerticalRuler.Origin = coordinate2Ruler;
        }
        #endregion

        public void LoadProcessDefinition(IProcess process)
        {
            this.m_ProcessDefinition = process;
            this.CoordinateCanvas.LoadProcess(process);
        }

        public IActivity AddActivity()
        {
            return null;
        }

        public IRoute AddRoute(IActivity from, IActivity to)
        {
            IRoute route = m_ProcessDefinition.GetRoute(from.Id, to.Id);
            if (route == null)
            {
                route = new Route(from.Id, to.Id);
                m_ProcessDefinition.Routes.Add(route);
                return route;
            }
            return route;
        }

        public IProcess ProcessDefinition
        {
            get { return this.m_ProcessDefinition; }
        }
    }
}
